package com.seabook.store.goodmgr.web;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.seabook.store.auth.web.UserSession;
import com.seabook.store.goodmgr.domain.Category;
import com.seabook.store.goodmgr.domain.Product;
import com.seabook.store.goodmgr.service.ProductService;
import com.seabook.store.util.ServiceLocator;

/**
 * 
 * @project WebStore
 * @author Tan Hao
 * @lastUpdate 2010-6-16
 */
@Controller
public class ProductController {

    private final ProductService productService;

    public ProductController() {
        this.productService = (ProductService) ServiceLocator
                .getBean("productService");
    }

    @InitBinder
    public void setAllowedFields(WebDataBinder dataBinder) {
        dataBinder.setDisallowedFields("id");
    }
    
    @RequestMapping(value = "/admin/products", method = RequestMethod.GET)
    public String getCategories(@RequestParam("categoryId") int categoryId,
            HttpServletRequest request) {
        request.getSession().setAttribute("categories", CategoryController.categories);
        request.setAttribute("curCategoryId", 0);
        request.setAttribute("productList", this.productService.getProductByCategory(0));
        return "products";
    }

    @RequestMapping(value = "admin/newproduct", method = RequestMethod.GET)
    public String setupForm(@RequestParam("categoryId") int categoryId,
            Model model) {
        Product product = new Product();
        if (0 != categoryId) {
            Category category = this.productService.getCategory(categoryId);
            product.setCategory(category);
            product.setCategoryId(categoryId);
        }
        model.addAttribute("product", product);
        return "productDetail";
    }
    
    @RequestMapping(value = "viewproduct", method = RequestMethod.GET)
    public String showProduct(@RequestParam("productId") int productId,
            HttpServletRequest request) {
        Product product = this.productService.getProductById(productId);
        request.setAttribute("product", product);
        return "product";
    }
    
    @RequestMapping(value = "admin/uploadproductimg", method = RequestMethod.GET)
    public String popUploadImage( @RequestParam("productId") int productId,
            Model model) {
        Product product = this.productService.getProductById(productId);
        model.addAttribute("product", product);
        return "uploadProductImage";
    }
    
    @RequestMapping(value = "admin/uploadproductimg", method = RequestMethod.POST)
    public String uploadProductImage( MultipartHttpServletRequest request) {
        int productId = Integer.parseInt(request.getParameter("productId"));
        String url = productService.saveProductImage(request.getFile("imageFile"), productId);
        Product product = this.productService.getProductById(productId);
        product.addImage(url);
        this.productService.saveProduct(product);
        request.setAttribute("product", product);
        return "productDetail";
    }
    
    @RequestMapping(value = "admin/editproduct", method = RequestMethod.GET)
    public String editProduct( @RequestParam("productId") int productId,
            Model model) {
        Product product = this.productService.getProductById(productId);
        model.addAttribute("product", product);
        return "productDetail";
    }

    @RequestMapping(value = "admin/saveproduct", method = RequestMethod.POST)
    public String processSubmit(@ModelAttribute("product") Product product,
            BindingResult result, SessionStatus status) {
        new ProductValidator().validate(product, result);
        if (result.hasErrors()) {
            return "productDetail";
        } else {
            this.productService.saveProduct(product);
            status.setComplete();
            return "redirect:/admin/products.do?categoryId=" + product.getCategory().getId();
        }
    }
}
